package com.asia5b.a5mall.shop.adapter

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.model.shop.AbsShopModel
import com.asia5b.a5mall.middleware.utils.WImageLoader
import com.asia5b.a5mall.middleware.utils.startGoodsDetailActivity
import com.asia5b.a5mall.middleware.utils.startShopActivity
import com.asia5b.a5mall.shop.R
import com.asia5b.a5mall.shop.databinding.ItemShoplistShopadapterBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * Created by zhangyuncai on 2020/1/11.
 */
class ShopListAdapter(val activity: AppCompatActivity, list: ArrayList<AbsShopModel>) :
        BaseQuickAdapter<AbsShopModel, BaseDataBindingHolder<ItemShoplistShopadapterBinding>>(R.layout.item_shoplist_shopadapter, list) {
    val goodsNum=4
    override fun convert(helper: BaseDataBindingHolder<ItemShoplistShopadapterBinding>, item: AbsShopModel) {
        val binding = helper.dataBinding!!
        binding.model = item
        WImageLoader.displayImage(item.absShopLogo, helper.getView(R.id.iv_collect_img), R.drawable.shop_default)
        val recyclerView = helper.getView<RecyclerView>(R.id.rv_shoplist_goods)

        val goods_list = item.absShopGoodsList

        //最多只展示3个
        var goodsList = arrayListOf<AbsGoodsModel>()
        if (!goods_list.isNullOrEmpty()) {
            if (goods_list!!.size > goodsNum) {
                for (index in 0 until goodsNum) {
                    goodsList.add(goods_list!![index])
                }
            } else {
                goodsList = goods_list
            }
        }
        if (!goodsList.isNullOrEmpty()) {
            val mAdapter = ShopListGoodsAdapter(goodsList)
            mAdapter.setOnItemClickListener { adapter, view, position ->
                val goodsModel=adapter.getItem(position) as AbsGoodsModel
                startGoodsDetailActivity(context,goodsModel.absGoodsId)
            }
            recyclerView.run {
                layoutManager = GridLayoutManager(context, goodsNum)!!
                adapter = mAdapter

            }
        }
        helper.setGone(R.id.rv_shoplist_goods, (goodsList.isNullOrEmpty()))
        helper.getView<ConstraintLayout>(R.id.cons_shop_vessel)
                .setOnClickListener {
                    startShopActivity(context,item.absShopId)
                }
    }
}